package org.example.hansp.sort;

import java.util.Arrays;
import java.util.Comparator;

public class ArraysSort {
    public static void main(String[] args) {
        Book[] books=new Book[4];
        books[0]=new Book("带壳的牡蛎是大人的心脏",59.8);
        books[1]=new Book("人间便利店",79.8);
        books[2]=new Book("如何孤独终老",49.9);
        books[3]=new Book("我永远也成为不了大人",99.9);
        Arrays.sort(books,new Comparator<Book>() {

            @Override
            public int compare(Book o1, Book o2) {
                double ve=o2.getPrice()-o1.getPrice();
                if(ve>0){
                    return 1;
                }else if(ve<0){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        System.out.println(Arrays.toString(books));

        Arrays.sort(books,new Comparator<Book>() {

            @Override
            public int compare(Book o1, Book o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        System.out.println(Arrays.toString(books));
    }
}
class Book{
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}